package blackeagle.ems.entities;

import blackeagle.ems.dal.DataClass;
import blackeagle.ems.dal.DataField;
import blackeagle.ems.dal.DataRecord;
import blackeagle.ems.dal.MethodType;
import blackeagle.ems.dal.Where;

/**
 * Project is the data class for managing projects.
 */
@DataClass(dataName="PROJECT")
public class Project extends DataRecord<Project>{
    private Integer id, departmentId, leadId, gameId;
    private String name, description;

    /**
     * The default 0-argument constructor. Creates a new instance of the Project-class.
     */
    public Project(){}
    
    /**
     * A static class containing read-only fields with column names.
     */
    public static class Columns{
        public static final String DepartmentId = "departmentid";
        public static final String Description = "description";
        public static final String GameId = "gameid";
        public static final String Id = "id";
        public static final String LeadId = "leadid";
        public static final String Name = "name";
        
        private Columns(){}
    }
    
    /**
     * Attempts to return the department to which the current project is linked.
     * @return A Department-record from the database if successful; otherwise <code>null</code>
     */
    public Department getDepartment(){
        Department dept = new Department().load(this.departmentId);
        return (dept.isNew()?null:dept);
    }
    
    /**
     * Attempts to set the department for this project.
     * @param department The department to link with the project.
     */
    public void setDepartment(Department department){
        if(department==null)return;
        
        Integer deptId = department.getId();
        this.setDepartmentId(deptId);
    }
    
    @DataField(fieldName="departmentid", methodType=MethodType.Getter)
    public Integer getDepartmentId() {
        return departmentId;
    }

    @DataField(fieldName="departmentid", methodType=MethodType.Setter)
    public void setDepartmentId(Integer departmentId) {
        this.departmentId = departmentId;
        this._isDirty=true;
    }

    @DataField(fieldName="description", methodType=MethodType.Getter)
    public String getDescription() {
        return description;
    }

    @DataField(fieldName="description", methodType=MethodType.Setter)
    public void setDescription(String description) {
        this.description = description;
        this._isDirty=true;
    }

    public Game getGame(){
        Game g = new Game().load(this.gameId);
        return (g.isNew()?null:g);
    }
    
    public void setGame(Game game){
        Integer gId = game.getId();
        this.setGameId(gId);
    }
    
    @DataField(fieldName="gameid", methodType=MethodType.Getter)
    public Integer getGameId() {
        return gameId;
    }

    @DataField(fieldName="gameid", methodType=MethodType.Setter)
    public void setGameId(Integer gameId) {
        this.gameId = gameId;
        this._isDirty=true;
    }

    @DataField(fieldName="id", methodType=MethodType.Getter, isPrimary=true, autoIncrement=true)
    public Integer getId(){
        return this.id;
    }
    
    @DataField(fieldName="id", methodType=MethodType.Setter, isPrimary=true, autoIncrement=true)
    public void setId(Integer value){
        if(value>0){
            this.id=value;
            this._isDirty=true;
        }        
    }

    public Employee getLead(){
        Employee e = new Employee().load(this.leadId);
        return (e.isNew()?null:e);
    }
    
    public void setLead(Employee lead){
        Integer lId = lead.getId();
        this.setLeadId(lId);
    }
    
    @DataField(fieldName="leadid", methodType=MethodType.Getter)
    public Integer getLeadId() {
        return leadId;
    }

    @DataField(fieldName="leadid", methodType=MethodType.Setter)
    public void setLeadId(Integer leadId) {
        this.leadId = leadId;
        this._isDirty=true;
    }

    @DataField(fieldName="name", methodType=MethodType.Getter)
    public String getName() {
        return name;
    }

    @DataField(fieldName="name", methodType=MethodType.Setter)
    public void setName(String name) {
        this.name = name;
        this._isDirty=true;
    }
    
    /**
     * Attempts to load all the sub-projects linked to this project.
     * @return The sub-projects of this project if successful; otherwise an empty SubprojectCollection.
     */
    public SubprojectCollection getSubprojects(){
        return new SubprojectCollection()
                .where(new Where(Subproject.Columns.ProjectId, this.id))
                .load();
    }
    
    @Override
    public String toString(){
        return this.name;
    }
}
